/*
 * @lc app=leetcode.cn id=856 lang=cpp
 *
 * [856] 括号的分数
 */
#include<vector>
using namespace std;
// @lc code=start
class Solution {
public:
    int scoreOfParentheses(string s) {
        vector<int> stack =vector<int>(s.size()/2,0);
        int sp=0;
        for (int i=0;i<s.size();++i){
            if (s[i]=='('){
                stack[sp]=-1;
                ++sp;
                continue;
            }
            if (stack[sp-1]==-1){
                --sp;
                if (sp==0||stack[sp-1]==-1){
                    stack[sp]=1;
                    ++sp;
                }else{
                     stack[sp-1]+=1;
                }
                continue;
            }
            stack[sp-2]=stack[sp-1]*2;
            --sp;
            if (sp>=2){
                if(stack[sp-2]!=-1){
                    stack[sp-2]+=stack[sp-1];
                    sp--;
                }
            }
        }
        return stack[0];
    }
};
// @lc code=end

